package com.macro.mall.common.redis;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.macro.mall.common.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wwj
 */
@Service
public class RedisLimitListService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public  void lPushScheme1(RedisLimitList redisLimitList, byte[] value) {

        int keyNum = 1; // eval的key的数量,为1
        byte[] arg1 = value;

        /**
         * // 队列最大长度
         */
        int arg2 = redisLimitList.getMaxLen();
        /**
         * // 队列key失效时间
         */
        int arg3 = redisLimitList.getExpireSeconds();
        String script = "local num = redis.call('llen',KEYS[1]);\n" +
                "    if tonumber(ARGV[2])>0 and tonumber(num) >= tonumber(ARGV[2]) then\n" +
                "        return -3\n" +
                "    end\n" +
                "    redis.call('lpush',KEYS[1],ARGV[1])\n" +
                "    if tonumber(ARGV[3]) > 0 then\n" +
                "        redis.call('expire', KEYS[1], ARGV[3])\n" +
                "    end\n" +
                "    local result = redis.call('llen',KEYS[1])\n" +
                "    return result";

        List<Object> args = new ArrayList<>();
        args.add(arg1);
        args.add(arg2);
        args.add(arg3);

    // 执行 lua 脚本
        DefaultRedisScript<Integer> defaultRedisScript = new DefaultRedisScript<>(script, Integer.class);
        Integer len = redisTemplate.execute(defaultRedisScript, (List<String>) new StringRedisSerializer(), new FastJsonRedisSerializer<>(Boolean.class), Collections.singleton(redisLimitList.getKey()), args);

    }


    public  void lPush(RedisLimitList redisLimitList, Long value) {

        String script = "local num = redis.call('llen',KEYS[1]);\n" +
                "    if tonumber(ARGV[2])>0 and tonumber(num) >= tonumber(ARGV[2]) then\n" +
                "        redis.call('rpop', KEYS[1])\n" +
                "    end\n" +
                "    redis.call('lpush',KEYS[1],ARGV[1])\n" +
                "    if tonumber(ARGV[3]) > 0 then\n" +
                "        redis.call('expire', KEYS[1], ARGV[3])\n" +
                "    end\n" +
                "    local result = redis.call('llen',KEYS[1])\n" +
                "    return result";


        Long arg1 = value;

        /**
         * // 队列最大长度
         */
        int arg2 = redisLimitList.getMaxLen();
        /**
         * // 队列key失效时间
         */
        int arg3 = redisLimitList.getExpireSeconds();

        List<Object> args = new ArrayList<>();
        args.add(arg1);
        args.add(arg2);
        args.add(arg3);

        // 执行 lua 脚本
        // 执行 lua 脚本
        DefaultRedisScript<Long> defaultRedisScript = new DefaultRedisScript<>(script, Long.class);
        Long len = redisTemplate.execute(defaultRedisScript, Collections.singletonList(redisLimitList.getKey()), arg1,arg2,arg3);


    }
}
